package com.eastfair.auth.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.entity.Organization;
import com.eastfair.auth.entity.OrganizationRole;
import com.eastfair.auth.entity.ResourceOrganization;
import com.eastfair.auth.enumeration.OrganizationOrgTypeEnum;
import com.eastfair.auth.model.OrganizationGroup;
import com.eastfair.auth.service.*;
import com.eastfair.auth.vo.OrganizationVo;
import com.eastfair.auth.vo.ResourceOrganizationVo;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.eastfair.core.base.R.SUCCESS_CODE;

/**
 * <p>
 * 公司组织操作业务实现类
 *
 * </p>
 *
 * @author ligang
 * @date 2021-07-16
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class ExhibitionOrganizationServiceImpl implements ExhibitionOrganizationService {
    @Autowired
    ResourceOrganizationService resourceOrganizationService;

    @Autowired
    CompanyOrganizationService companyOrganizationService;

    @Autowired
    OrganizationRoleService orgRoleService;

    @Autowired
    OrganizationService organizationService;


    @Override
    public R<Organization> updExhibition(OrganizationDTO organizationDTO) {
        //校验已经是否存在
        ResourceOrganization resourceOrganization = new ResourceOrganization();
        resourceOrganization.setResourceId(organizationDTO.getResourceId());
        List<ResourceOrganization> resourceOrganizationList = resourceOrganizationService.listOfResourceOrganization(resourceOrganization, null, null);
        if (resourceOrganizationList != null && !resourceOrganizationList.isEmpty()) {
            List<Long> orgIds = resourceOrganizationList.stream().map(ResourceOrganization::getOrganizationId).collect(Collectors.toList());
            Organization organization = new Organization();
            organization.setOrgType(OrganizationOrgTypeEnum.EXHIBITION);
            List<Organization> organizationList = organizationService.listOfOrganization(organization, orgIds);
            if (organizationList != null && !organizationList.isEmpty()) {
                //修改节点名字
                Organization organization1 = organizationList.get(0);
                organization1.setName(organizationDTO.getName());
                organizationService.updateByIdOfShard(organization1);
                //存在则直接返回
                return R.success(organization1);
            }
        }

        return R.success(null);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Organization> createExhibition(OrganizationDTO organizationDTO) {
        //查询会展id所在的组织
        Organization organization = findExibitionOrg(organizationDTO.getResourceId());
        if (organization != null) {
            return R.success(organization);
        }
        //获取项目顶级组织信息
        Long projectOrgId = null;
        List<ResourceOrganization> resourceOrganizationList = resourceOrganizationService.findResource(ContextUtil.getProjectId());
        if (resourceOrganizationList != null && !resourceOrganizationList.isEmpty()) {
            projectOrgId = resourceOrganizationList.get(0).getOrganizationId();
        }
        if (projectOrgId != null) {
            organizationDTO.setParentId(projectOrgId);
            organizationDTO.setOrgType(OrganizationOrgTypeEnum.EXHIBITION);
            R<Organization> r = organizationService.createOrg(organizationDTO);
            if (r.getCode() == SUCCESS_CODE) {
                //创建展会下的展商，观众，主办分类
                createClassifyOfExhibition(r.getData().getId());
            }
            return r;
        }
        return R.fail("操作失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R createClassifyOfExhibition(Long orgId) {
        //创建展会下的展商，观众，主办分类
        OrganizationVo organizationVo = organizationService.findAllById(orgId);
        //创建展会下的展商
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setName("展商组");
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.E_COMPANY_CLASSIFY);
        organizationDTO.setParentId(orgId);
        organizationDTO.setProjectId(organizationVo.getProjectId());
        organizationService.createOrg(organizationDTO);
        //创建展会下的观众
        organizationDTO = new OrganizationDTO();
        organizationDTO.setName("观众组");
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.E_AUDIENCE_CLASSIFY);
        organizationDTO.setParentId(orgId);
        organizationDTO.setProjectId(organizationVo.getProjectId());
        organizationService.createOrg(organizationDTO);
        //创建展会下的观众
        organizationDTO = new OrganizationDTO();
        organizationDTO.setName("主办组");
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.E_SPONSOR_CLASSIFY);
        organizationDTO.setParentId(orgId);
        organizationDTO.setProjectId(organizationVo.getProjectId());
        organizationService.createOrg(organizationDTO);
        return R.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Organization> addCompany(OrganizationDTO organizationDTO, Long exhibitionId) {
        //展会下的公司分类节点
        Organization companyOrg = findCompanyOfExibitionOrg(exhibitionId);
        //查询公司所在的所有组织
        List<ResourceOrganization> resourceOrganizationList = resourceOrganizationService.findResource(organizationDTO.getResourceId());
        if (resourceOrganizationList != null && !resourceOrganizationList.isEmpty()) {
            //查询组织信息
            List<Long> orgIds = resourceOrganizationList.stream().map(ResourceOrganization::getOrganizationId).collect(Collectors.toList());
            List<Organization> organizationList = organizationService.listOfOrganization(null, orgIds);
            for (Organization organization : organizationList) {
                if (organization.getCode().contains(companyOrg.getCode())) {
                    //包含在公司分类里
                    return R.success(organization);
                }
            }
        }
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.OTHER);
        organizationDTO.setParentId(companyOrg.getId());
        organizationDTO.setProjectId(companyOrg.getProjectId());
        return organizationService.createOrg(organizationDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R removeCompany(OrganizationDTO organizationDTO, Long exhibitionId) {
        //展会下的公司分类节点
        Organization companyOrg = findCompanyOfExibitionOrg(exhibitionId);
        //查询公司所在的所有组织
        List<ResourceOrganization> resourceOrganizationList = resourceOrganizationService.findResource(organizationDTO.getResourceId());
        if (resourceOrganizationList != null && !resourceOrganizationList.isEmpty()) {
            //查询组织信息
            List<Long> orgIds = resourceOrganizationList.stream().map(ResourceOrganization::getOrganizationId).collect(Collectors.toList());
            List<Organization> organizationList = organizationService.listOfOrganization(null, orgIds);
            for (Organization organization : organizationList) {
                if (organization.getCode().contains(companyOrg.getCode())) {
                    //删除
                    organizationService.del(organization.getId());
                    return R.success();
                }
            }
        }
        return R.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R removeUseridOfExhibitionCompany(Long resourceId, List<Long> removeUserIds) {
        return companyOrganizationService.removeUseridOfExhibition(resourceId, removeUserIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addUseridOfExhibitionCompany(Long resourceId, List<Long> userIds) {
        return companyOrganizationService.addUseridOfExhibition(resourceId, userIds);
    }

    @Override
    public Organization findExibitionOrg(Long exhibitionId) {
        //查询是否已经存在
        List<ResourceOrganization> resourceOrganizationList = resourceOrganizationService.findResource(exhibitionId);
        if (resourceOrganizationList != null && !resourceOrganizationList.isEmpty()) {
            List<Long> orgIds = resourceOrganizationList.stream().map(ResourceOrganization::getOrganizationId).collect(Collectors.toList());
            List<Organization> organizationList = organizationService.listOfOrganization(null, orgIds);
            for (Organization organization : organizationList) {
                if (organization.getOrgType().eq(OrganizationOrgTypeEnum.EXHIBITION)) {
                    return organization;
                }
            }
        }
        return null;
    }

    @Override
    public Organization findCompanyOfExibitionOrg(Long exhibitionId) {
        //查询会展id所在的组织
        Organization organization = findExibitionOrg(exhibitionId);
        if (organization == null) {
            return null;
        }
        //查询展会下的公司分类 节点
        List<Organization> organizationList = organizationService.queryOrgTypeOfParent(organization.getCode(), OrganizationOrgTypeEnum.E_COMPANY_CLASSIFY);
        if (organizationList == null || organizationList.isEmpty()) {
            return null;
        }
        return organizationList.get(0);
    }
}
